/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.model.items;

import com.aionengine.gameserver.controllers.observer.ActionObserver;
import com.aionengine.gameserver.controllers.observer.ObserverType;
import com.aionengine.gameserver.model.gameobjects.Creature;
import com.aionengine.gameserver.model.gameobjects.Item;
import com.aionengine.gameserver.model.gameobjects.PersistentState;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.network.aion.serverpackets.SM_INVENTORY_UPDATE_ITEM;
import com.aionengine.gameserver.utils.PacketSendUtility;

/**
 * @author ATracer
 */
public class ChargeInfo extends ActionObserver {

    public static final int LEVEL2 = 1000000;
    public static final int LEVEL1 = 500000;

    private int chargePoints;
    private final int attackBurn;
    private final int defendBurn;
    private final Item item;
    private Player player;

    /**
     * @param chargePoints
     */
    public ChargeInfo(int chargePoints, Item item) {
        super(ObserverType.ATTACK_DEFEND);
        this.chargePoints = chargePoints;
        this.item = item;
        if (item.getImprovement() != null) {
            this.attackBurn = item.getImprovement().getBurnAttack();
            this.defendBurn = item.getImprovement().getBurnDefend();
        } else {
            this.attackBurn = 0;
            this.defendBurn = 0;
        }
    }

    public int getChargePoints() {
        return this.chargePoints;
    }

    public void setPlayer(Player player) {
        this.player = player;
    }

    public int updateChargePoints(int addPoints) {
        int newChargePoints = chargePoints + addPoints;
        if (newChargePoints > LEVEL2) {
            newChargePoints = LEVEL2;
        } else if (newChargePoints < 0) {
            newChargePoints = 0;
        }
        if (item.isEquipped() && player != null)
            player.getEquipment().setPersistentState(PersistentState.UPDATE_REQUIRED);
        item.setPersistentState(PersistentState.UPDATE_REQUIRED);
        this.chargePoints = newChargePoints;
        return newChargePoints;
    }

    @Override
    public void attacked(Creature creature) {
        updateChargePoints(-defendBurn);
        Player player = this.player;
        if (player != null) {
            PacketSendUtility.sendPacket(player, new SM_INVENTORY_UPDATE_ITEM(player, item));
        }
    }

    @Override
    public void attack(Creature creature) {
        updateChargePoints(-attackBurn);
        Player player = this.player;
        if (player != null) {
            PacketSendUtility.sendPacket(player, new SM_INVENTORY_UPDATE_ITEM(player, item));
        }
    }

}
